package com.more.offer.algorithm.leetcode.lcode05;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yinpeng
 * @Date: 2019/9/27 15:44
 * @Description: TODO
 **/
public class Solution_01 {
	public static String longestPalindrome(String s) {
		if(s.equals("")){
			return "";
		}
		int startIndex=0,endIndex = 0;
		int length=1;
		Map<Character,Integer> map = new HashMap<>();
		for (int i = 0; i <s.length();i++) {
			if(!map.containsKey(s.charAt(i))){
				map.put(s.charAt(i),i);
			}
			Integer charStartIndex = map.get(s.charAt(i));
			if((i-charStartIndex+1)>length && (i-charStartIndex+1) != s.length()){
				if(isPalindromic(s.substring(charStartIndex,i+1))){
					startIndex = charStartIndex;
					endIndex = i;
					length = i-charStartIndex+1;
				}
			}
		}
		return s.substring(startIndex,endIndex+1);
	}

	public static boolean isPalindromic(String s) {
		int len = s.length();
		for (int i = 0; i < len / 2; i++) {
			if (s.charAt(i) != s.charAt(len - i - 1)) {
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {
		System.out.println(isPalindromic("bcbcb"));
		System.out.println(longestPalindrome("abbcbcb"));
	}
}
